/**
 * 新增修改完成调用 modalFormOk方法 编辑弹框组件ref定义为modalForm
 * 高级查询按钮调用 superQuery方法  高级查询组件ref定义为superQueryModal
 * data中url定义 list为查询列表  delete为删除单条记录  deleteBatch为批量删除
 */
import Vue from 'vue'
import {getAction} from "./action";
import {getToken} from "./auth";

export const JeecgListMixin = {
    data() {
        return {
            systemInfo : uni.getSystemInfoSync(),
            /* 查询条件-请不要在queryParam中声明非字符串值的属性 */
            queryParam: {},
            /* 数据源 */
            dataSource: [],
            /* 分页参数 */
            ipagination: {
                current: 1,
                pageSize: 10,
                pageSizeOptions: ['10', '20', '30'],
                showTotal: (total, range) => {
                    return range[0] + "-" + range[1] + " 共" + total + "条"
                },
                showQuickJumper: true,
                showSizeChanger: true,
                total: 0
            },
            /* 排序参数 */
            isorter: {
                column: '',
                order: '',
            },
            /* 筛选参数 */
            filters: {},
            /* table加载状态 */
            loading: false,
            /* table选中keys*/
            selectedRowKeys: [],
            /* table选中records*/
            selectionRows: [],
            /* 查询折叠 */
            toggleSearchStatus: false,
            /* 高级查询条件生效状态 */
            superQueryFlag: false,
            /* 高级查询条件 */
            superQueryParams: '',
            /** 高级查询拼接方式 */
            superQueryMatchType: 'and',
        }
    },
    created() {
        // if (!this.disableMixinCreated) {
        //     console.log(' -- mixin created -- ')
        //     // this.loadData();
        //     //初始化字典配置 在自己页面定义
        //     this.initDictConfig();
        // }
    },
    computed: {
        //token header
        tokenHeader() {
            let head = {'X-Access-Token': getToken()}
            // let tenantid = Vue.ls.get(TENANT_ID)
            // if(tenantid){
            //   head['tenant-id'] = tenantid
            // }
            return head;
        }
    },
    methods: {
        loadData(arg) {
            if (!this.url.list) {
                // this.$message.error("请设置url.list属性!")
                return
            }
            // this.lodData("loading")
            //加载数据 若传入参数1则加载第一页的内容
            if (arg === 1) {
                this.ipagination.current = 1;
            }
            var params = this.getQueryParams();//查询条件
            this.loading = true;
            getAction(this.url.list, params).then((res) => {
                if (res.success) {
                    //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
                    this.dataSource = res.result.records || res.result;
                    if (res.result.total) {
                        this.ipagination.total = res.result.total;
                    } else {
                        this.ipagination.total = 0;
                    }
                    //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
                }
            }).catch((error) => { // 2. 新增错误捕获
                this.lodData("noMore")
            }).finally(() => {
                this.loading = false
                if (this.systemInfo.platform === 'android' || this.systemInfo.platform === 'ios') {
                    this.lodData((this.dataSource.length < this.ipagination.pageSize) ? "noMore" : "loading")
                }
            })
        },
        initDictConfig() {
            console.log("--这是一个假的方法!")
        },
        getQueryParams() {
            //获取查询条件
            let sqp = {}
            if (this.superQueryParams) {
                sqp['superQueryParams'] = encodeURI(this.superQueryParams)
                sqp['superQueryMatchType'] = this.superQueryMatchType
            }
            if (this.groupBy) {
                this.queryParam.groupBy = this.groupBy
            }
            var param = Object.assign(sqp, this.queryParam, this.isorter, this.filters);
            param.pageNo = this.ipagination.current;
            param.pageSize = this.ipagination.pageSize;
            return this.filterObj(param);
        },
        searchQuery() {
            this.loadData(1);
            // 点击查询清空列表选中行
            // https://gitee.com/jeecg/jeecg-boot/issues/I4KTU1
            this.selectedRowKeys = []
            this.selectionRows = []
        },
        /**
         * 过滤对象中为空的属性
         * @param obj
         * @returns {*}
         */
        filterObj(obj) {
            if (!(typeof obj == 'object')) {
                return;
            }

            for (let key in obj) {
                if (obj.hasOwnProperty(key)
                    && (obj[key] == null || obj[key] == undefined || obj[key] === '')) {
                    delete obj[key];
                }
            }
            return obj;
        }
    }

}
